package mySql;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mySql.user_bean;

public class Login_Servlet extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		
		String login_name="root";
		String login_password="2014121167";
		String driverClass="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/test";
		Connection con=null;
		PreparedStatement pst=null;
		ResultSet rs=null;
		String strpwd=null;
		String strpwd_1=null;
		
		String username=request.getParameter("login_name");
		String password=request.getParameter("login_password");
		if(username==null||username.equals("")||password==null||password.equals(""))
		{
			response.setHeader("Refresh", "1;URL="+"http://localhost:8080/practise/jsp/login.jsp");
			out.println("name and password must write!");
		}
		else
		{
			DBConnection dbcon=new DBConnection(driverClass,url,login_password,login_name);
		
			try
			{
				con=dbcon.getConnection();
				//System.out.println("link ok!");
				pst=con.prepareStatement("select * from Login");
				rs=pst.executeQuery();
				while(rs.next())
				{
					strpwd=rs.getString(1);
					strpwd_1=rs.getString(2);
					if(strpwd.equals(username)&&strpwd_1.equals(password))
					break;	
				}
				if(strpwd!=null)
				{
				 if(strpwd.equals(username)&&strpwd_1.equals(password)&&username.equals("admin")&&password.equals("admin"))
					{
						response.setHeader("refresh", "3;URL="+"http://localhost:8080/practise/jsp/manage_publish.jsp");
						out.println("Welcome to Admin!");	
					}
					else if(strpwd.equals(username)&&strpwd_1.equals(password))
					{
							user_bean feng=new user_bean();
							feng.setUsername(username);
							feng.setPassword(password);
							HttpSession session=request.getSession();
							session.setAttribute("feng", feng);
							response.setHeader("refresh", "3;URL="+"http://localhost:8080/practise/jsp/head.jsp");
							out.println("Login Successful!");
					}
					else
					{
						response.setHeader("refresh", "2;URL="+"http://localhost:8080/practise/jsp/login.jsp");
						out.println("password is wrong!please input again!");
					}
				}
				else
				{
					response.setHeader("refresh", "2;URL="+"http://localhost:8080/practise/jsp/login.jsp");
					out.println("all is wrong!please input again!");	
				}			
			}
			
			catch (Exception e) {
				e.getStackTrace();
			}
		}
		
		
		
		
		
		out.close();
	}


}
